package com.cardbaobao.cardbabyclient.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.callback.RecycleItemClickedCallback;
import com.cardbaobao.cardbabyclient.ui.fragment.CreditFragment;
import com.cardbaobao.cardbabyclient.model.CreditCardIndexLayout;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import java.util.List;

/**
 * Created by Jiangzx on 14:25.
 */

public class HotRecPopularRankingDelegateAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder>{
    private int radian;
    private LayoutHelper mLayoutHelper;
    private List<CreditCardIndexLayout.PopularRanking> data;
    public HotRecPopularRankingDelegateAdapter(LayoutHelper layoutHelper,
                                               List<CreditCardIndexLayout.PopularRanking> data){
        this.mLayoutHelper=layoutHelper;
        this.data=data;
        this.radian=DensityUtil.dp2px(6.0f);
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    private RecycleItemClickedCallback clickedCallback;
    public void setRecycleItemClickedCallback(RecycleItemClickedCallback clickedCallback){
        this.clickedCallback=clickedCallback;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).
                inflate(R.layout.item_popular_ranking,parent,false);
        PopularRankingHolder holder = new PopularRankingHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final PopularRankingHolder rankingHolder = (PopularRankingHolder) holder;
        CreditCardIndexLayout.PopularRanking ranking=data.get(position);
        switch (position){
            case 0:
                rankingHolder.ivReason.setImageResource(R.mipmap.icon_number_one);
                rankingHolder.ivMedal.setImageResource(R.mipmap.icon_gold_medal);
                rankingHolder.tvReason.setTextColor(0xffffe034);
                rankingHolder.tvReason.setText(ranking.getDescription());
                rankingHolder.ivMedal.setVisibility(View.VISIBLE);
                rankingHolder.ivReason.setVisibility(View.VISIBLE);
                rankingHolder.tvReason.setVisibility(View.VISIBLE);
                break;
            case 1:
                rankingHolder.ivReason.setImageResource(R.mipmap.icon_number_two);
                rankingHolder.ivMedal.setImageResource(R.mipmap.icon_silver_medal);
                rankingHolder.tvReason.setTextColor(0xffd3e1ea);
                rankingHolder.tvReason.setText(ranking.getDescription());
                rankingHolder.ivMedal.setVisibility(View.VISIBLE);
                rankingHolder.ivReason.setVisibility(View.VISIBLE);
                rankingHolder.tvReason.setVisibility(View.VISIBLE);
                break;
            case 2:
                rankingHolder.ivReason.setImageResource(R.mipmap.icon_number_three);
                rankingHolder.ivMedal.setImageResource(R.mipmap.icon_bronze_medal);
                rankingHolder.tvReason.setTextColor(0xffd69982);
                rankingHolder.tvReason.setText(ranking.getDescription());
                rankingHolder.ivMedal.setVisibility(View.VISIBLE);
                rankingHolder.ivReason.setVisibility(View.VISIBLE);
                rankingHolder.tvReason.setVisibility(View.VISIBLE);
                break;
            default:
                rankingHolder.ivMedal.setVisibility(View.GONE);
                rankingHolder.ivReason.setVisibility(View.GONE);
                rankingHolder.tvReason.setVisibility(View.GONE);
                break;
        }
        rankingHolder.tvTitle.setText(ranking.getName());
        rankingHolder.tvMarketingOne.setText(ranking.getMarketing_one());
        rankingHolder.tvMarketTwo.setText(ranking.getMarketing_two());
        rankingHolder.tvApply.setText(ranking.getApply_count());
        ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL+ranking.getPicture_path(),
                rankingHolder.ivPic,
                R.mipmap.img_default_rectangle,
                radian);
        rankingHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(clickedCallback!=null){
                    clickedCallback.onRecycleItemClicked(position);
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        return CreditFragment.TYPE_POPULAR_RANKING;
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    //人气排行
    private class PopularRankingHolder extends RecyclerView.ViewHolder{
        private TextView tvTitle;
        private ImageView ivReason;
        private TextView tvReason;//上榜理由
        private TextView tvMarketingOne;
        private TextView tvMarketTwo;
        private ImageView ivMedal;
        private ImageView ivPic;
        private LinearLayout llApply;
        private TextView tvApply;
        private PopularRankingHolder(View itemView) {
            super(itemView);
            tvTitle= (TextView) itemView.findViewById(R.id.tv_title);
            ivReason= (ImageView) itemView.findViewById(R.id.iv_reason);
            tvReason= (TextView) itemView.findViewById(R.id.tv_reason);
            tvMarketingOne= (TextView) itemView.findViewById(R.id.tv_marketing_one);
            tvMarketTwo= (TextView) itemView.findViewById(R.id.tv_marketing_two);
            ivMedal= (ImageView) itemView.findViewById(R.id.iv_medal);
            ivPic= (ImageView) itemView.findViewById(R.id.iv_pic);
            llApply= (LinearLayout) itemView.findViewById(R.id.ll_apply);
            tvApply= (TextView) itemView.findViewById(R.id.tv_number);
        }
    }
}
